--[[
Aero By Indigorose Forum members,
T3STY, Imagine Programming, LucasTheDuck
Sim, and gyourl for requesting this function.

http://www.indigorose.com/forums


All the code here is AS IS and I wont guarantee
any of it to work or not to work, it works for me

License (MIT):
http://code.google.com/p/simplyraero/wiki/License
]]--


-- This is the Simply R - Aero version of the code 
-- see added function called SetColor(color, title);
-- see Comments #1 for the clean code.
function ExtendFrameIntoClientArea(nWndHnd, nLeft, nRight, nTop, nBottom)
	local sOSName = System.GetOSName();
	local nReturn = 0; -- Returns 0 if DWM Composition is disabled, one of the function returned an error, or the wrong OS is running for this.
	local sError  = "";
	if(sOSName=="Windows Vista")or(sOSName=="Windows 7")then
		Memory.OpenLibrary(1, "dwmapi.dll")
		local lpdwEnabled = Memory.Allocate(4);
		if(lpdwEnabled>0)then
			local F_DWM_ICE = Memory.OpenFunction(1, "DwmIsCompositionEnabled", DLL_CALL_STDCALL, DLL_RETURN_VOID);
			local s_ice = Memory.CallFunction(F_DWM_ICE,lpdwEnabled);
			if(Memory.GetInt(lpdwEnabled)>0)then
				lptrMARGIN = Memory.CreateStructure("int,int,int,int");
				if(lptrMARGIN>0)then
					SetColor(Math.HexColorToNumber("666699"), "Simply R - Aero");
					SetLayeredWindowAttributes(nWndHnd, Math.HexColorToNumber("666699"));
					Memory.SetStructureData(lptrMARGIN, 1, 0, nLeft, "");
					Memory.SetStructureData(lptrMARGIN, 2, 0, nRight, "");
					Memory.SetStructureData(lptrMARGIN, 3, 0, nTop, "");
					Memory.SetStructureData(lptrMARGIN, 4, 0, nBottom, "");
					local F_DWM_EFICA = Memory.OpenFunction(1, "DwmExtendFrameIntoClientArea", DLL_CALL_STDCALL, DLL_RETURN_VOID);
					if(Memory.CallFunction(F_DWM_EFICA, nWndHnd, lptrMARGIN)==0)then -- Returns S_OK (0)
						nReturn = 1;
						sError = "OK";
					else
						SetColor(Math.HexColorToNumber("F0F0F0"), "Simply R - Basic");
						sError = "DwmExtendFrameIntoClientAre returned an error code!"; 
					end
					Memory.Free(lptrMARGIN); -- Free it, DWM copies the data.
				else
					SetColor(Math.HexColorToNumber("F0F0F0"), "Simply R - Basic");
					sError = "Could not allocate MARGIN structure!";
				end
			else
				SetColor(Math.HexColorToNumber("F0F0F0"), "Simply R - Basic");
				sError = "Dwm Composition is not enabled!";
			end
			Memory.Free(lpdwEnabled); -- Free those 4 bytes too ofcourse.
		else
			SetColor(Math.HexColorToNumber("F0F0F0"), "Simply R - Basic");
			sError = "Could not allocate memory for lpdwEnabled";
		end
		Memory.CloseLibrary(1);
	end
	return nReturn,sError;
end
-- Example Call:
-- ExtendFrameIntoClientArea(Application.GetWndHandle(), 0, 0, 0, -1); 


-------------------------------------------------------------------------------------------------------------------------------------------------


-- This function was by Retest its full action I am unsure part from it works
-- As you can see I added this function to the Aero function on line 34 this 
-- if using the Comment #1 version you would call before that function.
-- This function as not been edited no Comment section for this functions.
function SetLayeredWindowAttributes(hwnd, Color)	
	local ret = tonumber(DLL.CallFunction("user32.dll", "GetWindowLongA",  hwnd..", -20", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL)) + 524288
	result = DLL.CallFunction("user32.dll", "SetWindowLongA", hwnd..", -20, "..ret, DLL_RETURN_TYPE_INTEGER, DLL_CALL_STDCALL);
	DLL.CallFunction("user32.dll", "SetLayeredWindowAttributes",  hwnd..", "..Color..", 0, 1", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL);
end
-- Example Call:
-- SetLayeredWindowAttributes(nWndHnd, Math.HexColorToNumber("666699"));


------------------------------------------------------------------------------------------------------------------------------------------------------

-- This function is a simple color and title switch I set up just incase aero was not
-- enabled, this is NOT in the default aero function ever.
function SetColor(sColor, sText)
	Window.SetText(Application.GetWndHandle(), sText);
	TbSet = {};
	NewColor = sColor;
	TbSet.BackgroundColor = NewColor;
	Application.SetPageProperties("Core.Main", TbSet);
end
-- Example Call:
-- SetColor(Math.HexColorToNumber("666699"), "App Title - Aero Mode");


--[[ COMMENT #1
This is the default function.

function ExtendFrameIntoClientArea(nWndHnd, nLeft, nRight, nTop, nBottom)
	local sOSName = System.GetOSName();
	local nReturn = 0; -- Returns 0 if DWM Composition is disabled, one of the function returned an error, or the wrong OS is running for this.
	local sError  = "";
	if(sOSName=="Windows Vista")or(sOSName=="Windows 7")then
		Memory.OpenLibrary(1, "dwmapi.dll")
		local lpdwEnabled = Memory.Allocate(4);
		if(lpdwEnabled>0)then
			local F_DWM_ICE = Memory.OpenFunction(1, "DwmIsCompositionEnabled", DLL_CALL_STDCALL, DLL_RETURN_VOID);
			local s_ice = Memory.CallFunction(F_DWM_ICE,lpdwEnabled);
			if(Memory.GetInt(lpdwEnabled)>0)then
				lptrMARGIN = Memory.CreateStructure("int,int,int,int");
				if(lptrMARGIN>0)then
					Memory.SetStructureData(lptrMARGIN, 1, 0, nLeft, "");
					Memory.SetStructureData(lptrMARGIN, 2, 0, nRight, "");
					Memory.SetStructureData(lptrMARGIN, 3, 0, nTop, "");
					Memory.SetStructureData(lptrMARGIN, 4, 0, nBottom, "");
					local F_DWM_EFICA = Memory.OpenFunction(1, "DwmExtendFrameIntoClientArea", DLL_CALL_STDCALL, DLL_RETURN_VOID);
					if(Memory.CallFunction(F_DWM_EFICA, nWndHnd, lptrMARGIN)==0)then -- Returns S_OK (0)
						nReturn = 1;
						sError = "OK";
					else
						sError = "DwmExtendFrameIntoClientAre returned an error code!"; 
					end
					Memory.Free(lptrMARGIN); -- Free it, DWM copies the data.
				else
					sError = "Could not allocate MARGIN structure!";
				end
			else
				sError = "Dwm Composition is not enabled!";
			end
			Memory.Free(lpdwEnabled); -- Free those 4 bytes too ofcourse.
		else
			sError = "Could not allocate memory for lpdwEnabled";
		end
		Memory.CloseLibrary(1);
	end
	return nReturn,sError;
end
]]--